<?php
include("load-settings.php");

function romanToNumber($roman)
 {
	 $romans = array(
    'M' => 1000,
    'CM' => 900,
    'D' => 500,
    'CD' => 400,
    'C' => 100,
    'XC' => 90,
    'L' => 50,
    'XL' => 40,
    'X' => 10,
    'IX' => 9,
    'V' => 5,
    'IV' => 4,
    'I' => 1,
);

$result = 0;

foreach ($romans as $key => $value) {
    while (strpos($roman, $key) === 0) {
        $result += $value;
        $roman = substr($roman, strlen($key));
    }
}
return $result;
}
require_once("unirest/lib/Unirest.php");

$order_result = mysql_query("SELECT * FROM order_record WHERE verified_complete = 0");

while($order_row = mysql_fetch_array($order_result))
{
	$result = mysql_query("SELECT * FROM user WHERE id = ".$order_row['user']);
	$row = mysql_fetch_array($result);
	$summoner = $order_row['summoner'];
	$region = strtolower($order_row['region']);
	$user_name = $row['username'];
	$password = $row['password'];

	$response = Unirest::get(
	  "https://teemojson.p.mashape.com/player/$region/".str_replace(" ", "", $summoner)."/recent_games",
	  array(
		"X-Mashape-Authorization" => "XcM4wvOlSewxmSNGstMYMpkjxvZGc8gx"
	  )
	);

	$returnArray = array();

	$decode = json_decode($response->raw_body)->data->gameStatistics->array;

	$x = 0;
	$y = 0;

	while(isset($decode[$x]))
	{
		if($decode[$x]->ranked)
		{
			$match_array = array();

			$match_array['champion'] = $decode[$x]->championId;
			$match_array['date'] = $decode[$x]->createDate;
			$match_array['timestamp'] = strtotime($decode[$x]->createDate);
			$match_array['elo'] = $decode[$x]->eloChange;
			$match_array['spell1'] = $decode[$x]->spell1;
			$match_array['spell2'] = $decode[$x]->spell2;
			$match_array['deaths'] = 0;

			$match_array['kills'] = 0;

			$match_array['assists'] = 0;

			$match_array['minions'] = 0;

			$match_array['gold'] = 0;

			$match_array['item0'] = "NULL";

			$match_array['item1'] = "NULL";

			$match_array['item2'] = "NULL";

			$match_array['item3'] = "NULL";

			$match_array['item4'] = "NULL";

			$match_array['item5'] = "NULL";

			$match_array['win'] = 0;

			foreach($decode[$x]->statistics->array as $stat)
			{
				if($stat->statType == "CHAMPIONS_KILLED")
				{
					if(!isset($stat->value) || $stat->value == "")
						$match_array['kills'] = 0;
					else
						$match_array['kills'] = $stat->value;
				}

				if($stat->statType == "ASSISTS")
				{
					if(!isset($stat->value) || $stat->value == "")
						$match_array['assists'] = 0;
					else
						$match_array['assists'] = $stat->value;
				}

				if($stat->statType == "WIN")
				{
					if(!isset($stat->value) || $stat->value == "")
						$match_array['win'] = 0;
					else
						$match_array['win'] = $stat->value;
				}

				if($stat->statType == "GOLD_EARNED")
				{
					if(!isset($stat->value) || $stat->value == "")
						$match_array['gold'] = 0;
					else
						$match_array['gold'] = $stat->value;
				}

				if($stat->statType == "MINIONS_KILLED")
				{
					if(!isset($stat->value) || $stat->value == "")
						$match_array['minions'] = 0;
					else
						$match_array['minions'] = $stat->value;
				}

				if($stat->statType == "NUM_DEATHS")
				{
					if(!isset($stat->value) || $stat->value == "")
						$match_array['deaths'] = 0;
					else
						$match_array['deaths'] = $stat->value;
				}

				if($stat->statType == "ITEM0")
				{
					$match_array['item0'] = $stat->value;
				}
				if($stat->statType == "ITEM1")
				{
					$match_array['item1'] = $stat->value;
				}
				if($stat->statType == "ITEM2")
				{
					$match_array['item2'] = $stat->value;
				}
				if($stat->statType == "ITEM3")
				{
					$match_array['item3'] = $stat->value;
				}
				if($stat->statType == "ITEM4")
				{
					$match_array['item4'] = $stat->value;
				}
				if($stat->statType == "ITEM5")
				{
					$match_array['item5'] = $stat->value;
				}

				
			}
			if($match_array['timestamp'] >= $order_row['timestamp'])
			{
				array_push($returnArray, $match_array);
				$y++;
			}
		}
		$x++;
	}

	usort($returnArray, function($a, $b) {
		return $a['timestamp'] - $b['timestamp'];
	});

	$x = 0;

	$newresponse = Unirest::get(
	  "https://teemojson.p.mashape.com/player/$region/".str_replace(" ", "", $summoner)."/leagues",
	  array(
		"X-Mashape-Authorization" => "XcM4wvOlSewxmSNGstMYMpkjxvZGc8gx"
	  ),
	  null
	);

	$newdecode = json_decode($newresponse->raw_body)->data->summonerLeagues;
	$y = 0;
	$lp_at_end = 0;
	$newwins = 0;
	$newlosses = 0;

	while(isset($newdecode->array[$y]))
	{
		$i = 0;

		while(isset($newdecode->array[$y]->entries->array[$i]))
		{
			if(str_replace(" ", "", strtolower($newdecode->array[$y]->entries->array[$i]->playerOrTeamName)) == strtolower($summoner))
			{
				$newwins = $newdecode->array[$y]->entries->array[$i]->wins;
				$newlosses = $newdecode->array[$y]->entries->array[$i]->losses;
				$lp_at_end = $newdecode->array[$y]->entries->array[$i]->leaguePoints;
				break;
			}
			$i++;
		}

		$y++;
	}

	$ending_stats = $newwins." wins, ".$newlosses." losses";

	/*$response = Unirest::get(
	  "https://teemojson.p.mashape.com/player/".$region."/".str_replace(" ", "", $summoner)."/leagues",
	  array(
		"X-Mashape-Authorization" => "XcM4wvOlSewxmSNGstMYMpkjxvZGc8gx"
	  )
	);*/

	$statdecode = json_decode($newresponse->raw_body);

	$y = 0;

	$tier = "UNRANKED";
	$rank = "";

	while(isset($statdecode->data->summonerLeagues->array[$y]))
	{
		if($statdecode->data->summonerLeagues->array[$y]->queue == "RANKED_SOLO_5x5")
		{
			$tier = $statdecode->data->summonerLeagues->array[$y]->tier;
			$rank = $statdecode->data->summonerLeagues->array[$y]->requestorsRank;
		}
		$y++;
	}

	while(isset($returnArray[$x]))
	{
		$booster_result = mysql_query("SELECT * FROM booster_order WHERE active = 1 AND order_record = ".$order_row['id']) or die(mysql_error());
		if(mysql_num_rows($booster_result) != 0)
		{
			$booster_row = mysql_fetch_array($booster_result);

			if($returnArray[$x]['timestamp'] > $booster_row['last_match'])
			{		
				$booster_detail = mysql_query("SELECT * FROM user WHERE id = ".$booster_row['booster']);
				$booster_detail_row = mysql_fetch_array($booster_detail);

				mysql_query("INSERT INTO match_history (order_id, champion, timestamp, spell1, spell2, kills, assists, win, gold, minions, deaths, item0, item1, item2, item3, item4, item5, lp_at_end, date, tier_at_end, rank_at_end, booster) VALUES (".$order_row['id'].", ".$returnArray[$x]['champion'].", ".$returnArray[$x]['timestamp'].", ".$returnArray[$x]['spell1'].", ".$returnArray[$x]['spell2'].", ".$returnArray[$x]['kills'].", ".$returnArray[$x]['assists'].", ".$returnArray[$x]['win'].", ".$returnArray[$x]['gold'].", ".$returnArray[$x]['minions'].", ".$returnArray[$x]['deaths'].", ".$returnArray[$x]['item0'].", ".$returnArray[$x]['item1'].", ".$returnArray[$x]['item2'].", ".$returnArray[$x]['item3'].", ".$returnArray[$x]['item4'].", ".$returnArray[$x]['item5'].", ".$lp_at_end.", '".$returnArray[$x]['date']."', '$tier', '$rank', '".$booster_detail_row['username']."')") or die(mysql_error());

				if($returnArray[$x]['win'] == 1)
				{
					$wins = $booster_row['wins'];
					$wins++;
			
					mysql_query("UPDATE booster_order SET wins = $wins WHERE id = ".$booster_row['id']) or die(mysql_error());

					if($order_row['end_tier'] == null && $wins - $booster_row['losses'] >= $order_row['number_of_wins'])
					{
						mysql_query("UPDATE order_record SET complete = 1, ending_stats = '$ending_stats' WHERE id = ".$order_row['id']) or die(mysql_error());
						mysql_query("UPTADE booster_order SET status = 3 WHERE id = ".$booster_row['id']);
					}
					else if($order_row['end_tier'] != '' && $order_row['end_tier'] != null)
					{
						if(strtolower($tier) == strtolower($order_row['end_tier']) && $order_row['end_division'] == romanToNumber($rank))
						{

							mysql_query("UPDATE order_record SET complete = 1, ending_stats = '$ending_stats' WHERE id = ".$order_row['id']) or die(mysql_error());
							mysql_query("UPTADE booster_order SET status = 3 WHERE id = ".$booster_row['id']);
						}
					}
				}
				else
				{

					$losses = $booster_row['losses'];
					$losses++;

					mysql_query("UPDATE booster_order SET losses = $losses WHERE id = ".$booster_row['id']) or die(mysql_error());
				}

				mysql_query("UPDATE booster_order SET last_match = ".$returnArray[$x]['timestamp']." WHERE id = ".$booster_row['id']) or die(mysql_error());
			}
		}

		$x++;
	}
}

$order_result = mysql_query("SELECT * FROM duo_order WHERE verified_complete = 0");

while($order_row = mysql_fetch_array($order_result))
{
	$result = mysql_query("SELECT * FROM user WHERE id = ".$order_row['user']);
	$row = mysql_fetch_array($result);
	$summoner = $order_row['summoner'];
	$region = strtolower($order_row['region']);
	$user_name = $row['username'];
	$password = $row['password'];

	$response = Unirest::get(
	  "https://teemojson.p.mashape.com/player/$region/".str_replace(" ", "", $summoner)."/recent_games",
	  array(
		"X-Mashape-Authorization" => "XcM4wvOlSewxmSNGstMYMpkjxvZGc8gx"
	  )
	);

	$returnArray = array();

	$decode = json_decode($response->raw_body)->data->gameStatistics->array;

	$x = 0;
	$y = 0;

	$newresponse = Unirest::get(
	  "https://teemojson.p.mashape.com/player/$region/".str_replace(" ", "", $summoner)."/leagues",
	  array(
		"X-Mashape-Authorization" => "XcM4wvOlSewxmSNGstMYMpkjxvZGc8gx"
	  ),
	  null
	);

	$newdecode = json_decode($newresponse->raw_body)->data->summonerLeagues->array[0]->entries->array;
	$y = 0;
	$lp_at_end = 0;
	$newwins = 0;
	$newlosses = 0;

	while(isset($newdecode[$y]))
	{
		if(str_replace(" ", "", strtolower($newdecode[$y]->playerOrTeamName)) == strtolower($summoner))
		{
			$newwins = $newdecode[$y]->wins;
			$newlosses = $newdecode[$y]->losses;
			$lp_at_end = $newdecode[$y]->leaguePoints;
			break;
		}

		$y++;
	}

	$ending_stats = $newwins." wins, ".$newlosses." losses";

	/*$response = Unirest::get(
	  "https://teemojson.p.mashape.com/player/".$region."/".str_replace(" ", "", $summoner)."/leagues",
	  array(
		"X-Mashape-Authorization" => "XcM4wvOlSewxmSNGstMYMpkjxvZGc8gx"
	  )
	);*/

	$statdecode = json_decode($newresponse->raw_body);

	$y = 0;

	$tier = "UNRANKED";
	$rank = "";

	while(isset($statdecode->data->summonerLeagues->array[$y]))
	{
		if($statdecode->data->summonerLeagues->array[$y]->queue == "RANKED_SOLO_5x5")
		{
			$tier = $statdecode->data->summonerLeagues->array[$y]->tier;
			$rank = $statdecode->data->summonerLeagues->array[$y]->requestorsRank;
		}
		$y++;
	}

	while(isset($decode[$x]))
	{
		if($decode[$x]->ranked)
		{
			$match_array = array();

			$match_array['champion'] = $decode[$x]->championId;
			$match_array['date'] = $decode[$x]->createDate;
			$match_array['timestamp'] = strtotime($decode[$x]->createDate);
			$match_array['elo'] = $decode[$x]->eloChange;
			$match_array['spell1'] = $decode[$x]->spell1;
			$match_array['spell2'] = $decode[$x]->spell2;

			$match_array['kills'] = 0;

			$match_array['assists'] = 0;

			$match_array['minions'] = 0;

			$match_array['gold'] = 0;

			$match_array['item0'] = "NULL";

			$match_array['item1'] = "NULL";

			$match_array['item2'] = "NULL";

			$match_array['item3'] = "NULL";

			$match_array['item4'] = "NULL";

			$match_array['item5'] = "NULL";

			$match_array['win'] = 0;

			foreach($decode[$x]->statistics->array as $stat)
			{
				if($stat->statType == "CHAMPIONS_KILLED")
				{
					if(!isset($stat->value) || $stat->value == "")
						$match_array['kills'] = 0;
					else
						$match_array['kills'] = $stat->value;
				}

				if($stat->statType == "ASSISTS")
				{
					if(!isset($stat->value) || $stat->value == "")
						$match_array['assists'] = 0;
					else
						$match_array['assists'] = $stat->value;
				}

				if($stat->statType == "WIN")
				{
					if(!isset($stat->value) || $stat->value == "")
						$match_array['win'] = 0;
					else
						$match_array['win'] = $stat->value;
				}

				if($stat->statType == "GOLD_EARNED")
				{
					if(!isset($stat->value) || $stat->value == "")
						$match_array['gold'] = 0;
					else
						$match_array['gold'] = $stat->value;
				}

				if($stat->statType == "MINIONS_KILLED")
				{
					if(!isset($stat->value) || $stat->value == "")
						$match_array['minions'] = 0;
					else
						$match_array['minions'] = $stat->value;
				}

				if($stat->statType == "NUM_DEATHS")
				{
					if(!isset($stat->value) || $stat->value == "")
						$match_array['deaths'] = 0;
					else
						$match_array['deaths'] = $stat->value;
				}

				if($stat->statType == "ITEM0")
				{
					$match_array['item0'] = $stat->value;
				}
				if($stat->statType == "ITEM1")
				{
					$match_array['item1'] = $stat->value;
				}
				if($stat->statType == "ITEM2")
				{
					$match_array['item2'] = $stat->value;
				}
				if($stat->statType == "ITEM3")
				{
					$match_array['item3'] = $stat->value;
				}
				if($stat->statType == "ITEM4")
				{
					$match_array['item4'] = $stat->value;
				}
				if($stat->statType == "ITEM5")
				{
					$match_array['item5'] = $stat->value;
				}

				if(!isset($match_array['deaths']))
				{
					$match_array['deaths'] = 0;
				}
				if(!isset($match_array['kills']))
				{
					$match_array['kills'] = 0;
				}
				if(!isset($match_array['assists']))
				{
					$match_array['assists'] = 0;
				}
				if(!isset($match_array['minions']))
				{
					$match_array['minions'] = 0;
				}
				if(!isset($match_array['gold']))
				{
					$match_array['gold'] = 0;
				}

				if(!isset($match_array['item0']))
				{
					$match_array['item0'] = "NULL";
				}
				if(!isset($match_array['item1']))
				{
					$match_array['item1'] = "NULL";
				}
				if(!isset($match_array['item2']))
				{
					$match_array['item2'] = "NULL";
				}
				if(!isset($match_array['item3']))
				{
					$match_array['item3'] = "NULL";
				}
				if(!isset($match_array['item4']))
				{
					$match_array['item4'] = "NULL";
				}
				if(!isset($match_array['item5']))
				{
					$match_array['item5'] = "NULL";
				}
			}
			if($match_array['timestamp'] >= $order_row['timestamp'])
			{
				array_push($returnArray, $match_array);
				$y++;
			}
		}
		$x++;
	}

	usort($returnArray, function($a, $b) {
		return $a['timestamp'] - $b['timestamp'];
	});

	$x = 0;

	

	while(isset($returnArray[$x]))
	{
		$booster_result = mysql_query("SELECT * FROM booster_duo WHERE active = 1 AND duo_order = ".$order_row['id']) or die(mysql_error());
		if(mysql_num_rows($booster_result) != 0)
		{
			$booster_row = mysql_fetch_array($booster_result);

			if($returnArray[$x]['timestamp'] > $booster_row['last_match'])
			{
				$booster_detail = mysql_query("SELECT * FROM user WHERE id = ".$booster_row['booster']);
				$booster_detail_row = mysql_fetch_array($booster_detail);

				mysql_query("INSERT INTO match_history (order_id, champion, timestamp, spell1, spell2, kills, assists, win, gold, minions, deaths, item0, item1, item2, item3, item4, item5, lp_at_end, date, tier_at_end, rank_at_end, booster) VALUES (".$order_row['id'].", ".$returnArray[$x]['champion'].", ".$returnArray[$x]['timestamp'].", ".$returnArray[$x]['spell1'].", ".$returnArray[$x]['spell2'].", ".$returnArray[$x]['kills'].", ".$returnArray[$x]['assists'].", ".$returnArray[$x]['win'].", ".$returnArray[$x]['gold'].", ".$returnArray[$x]['minions'].", ".$returnArray[$x]['deaths'].", ".$returnArray[$x]['item0'].", ".$returnArray[$x]['item1'].", ".$returnArray[$x]['item2'].", ".$returnArray[$x]['item3'].", ".$returnArray[$x]['item4'].", ".$returnArray[$x]['item5'].", ".$lp_at_end.", '".$returnArray[$x]['date']."', '$tier', '$rank', '".$booster_detail_row['username']."')") or die(mysql_error());

				mysql_query("UPDATE booster_duo SET last_match = ".$returnArray[$x]['timestamp']." WHERE id = ".$booster_row['id']) or die(mysql_error());
			}
		}

		$x++;
	}

}

$order_result = mysql_query("SELECT * FROM leveling_order WHERE complete = 0");

while($order_row = mysql_fetch_array($order_result))
{
	$newresponse = Unirest::get(
	  "https://teemojson.p.mashape.com/player/".$order_row['region']."/".str_replace(" ", "", $order_row['summoner']),
	  array(
		"X-Mashape-Authorization" => "XcM4wvOlSewxmSNGstMYMpkjxvZGc8gx"
	  ),
	  null
	);
	$current_level = json_decode($newresponse->raw_body)->data->level;

	mysql_query("UPDATE leveling_order SET current_level = $current_level WHERE id = ".$order_row['id']);
}

$champ_lookup = array();

$response = Unirest::get(
  "https://teemojson.p.mashape.com/datadragon/champion",
  array(
    "X-Mashape-Authorization" => "XcM4wvOlSewxmSNGstMYMpkjxvZGc8gx"
  )
);

$decode = json_decode($response->raw_body)->data;
$x = 0;

foreach ($decode as $item)
{
	$champ_lookup[$x] = array();
	$champ_lookup[$x]['id'] = $item->key;
	$champ_lookup[$x]['name'] = $item->id;
	$x++;
}

$x = 0;

while(isset($champ_lookup[$x]))
{
	$result = mysql_query("SELECT * FROM champion WHERE id = ".$champ_lookup[$x]['id']);

	if(mysql_num_rows($result) == 0)
		mysql_query("INSERT INTO champion (id, name) VALUES (".$champ_lookup[$x]['id'].", '".$champ_lookup[$x]['name']."')");

	$x++;
}
?>